package cn.wolfcode.shop.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;

import cn.wolfcode.shop.domain.Catalog;
import cn.wolfcode.shop.domain.Property;
import cn.wolfcode.shop.domain.PropertyValue;
import cn.wolfcode.shop.domain.SkuProperty;
import cn.wolfcode.shop.service.ICatalogService;
import cn.wolfcode.shop.service.IPropertyService;
import cn.wolfcode.shop.service.IPropertyValueService;
import cn.wolfcode.shop.service.ISkuPropertyService;
import cn.wolfcode.shop.vo.JSONResult;

@Controller
@RequestMapping(value = "/catalogs")
public class CatalogController {

	@Reference
	private ICatalogService catalogService;

	@Reference
	private IPropertyService propertyService;

	@Reference
	private IPropertyValueService propertyValueService;
	
	@Reference
	private ISkuPropertyService skuPropertyService;

	/**
	 * 获取所有商品分类
	 * @return
	 */
	@RequestMapping("/catalogList")
	@ResponseBody
	public List<Catalog> getCatalogList() {
		return catalogService.getCataloglist();
	}

	/**
	 * 商品分类管理
	 * @param model
	 * @return
	 */
	@RequestMapping("/view")
	public String catalogView(Model model) {
		List<Catalog> cataloglist = catalogService.getCataloglist();
		String catalogJson = JSON.toJSONString(cataloglist);
		model.addAttribute("allCatalog", catalogJson);
		return "catalog/catalog";
	}

	/**
	 * 通过分类id,获取分类下的子分类
	 * @param catalogId
	 * @return
	 */
	@RequestMapping("/{catalogId}/childs")
	//	public String childCatalog(Long catalogId) {
	public String childCatalog(@PathVariable("catalogId") Long id, Model model) {
		List<Catalog> childCatalogList = catalogService.getChildCatalog(id);
		model.addAttribute("catalogList", childCatalogList);
		return "catalog/child_catalog";
	}

	/**
	 * 将前台传入的json字符串,通过@RequestBody将json转成对象
	 * @param ids
	 * @return
	 */
	@PatchMapping
	@ResponseBody
	public JSONResult sortCatalog(@RequestBody List<Long> ids) {
		JSONResult jsonResult = new JSONResult();
		try {
			catalogService.sortCatalog(ids);
		} catch (Exception e) {
			e.printStackTrace();
			jsonResult.setMessage("更新异常");
		}
		return jsonResult;
	}

	/**
	 * 新增或保存
	 * @param catalog
	 * @return
	 */
	@PostMapping
	@ResponseBody
	public JSONResult save(Catalog catalog) {
		JSONResult jsonResult = new JSONResult();
		try {
			catalogService.saveOrUpdate(catalog);
		} catch (Exception e) {
			e.printStackTrace();
			jsonResult.setMessage(e.getMessage());
		}
		return jsonResult;
	}

	/**
	 * 通过主键删除对象
	 * @return
	 */
	@DeleteMapping("/{catalogId}")
	@ResponseBody
	public JSONResult delete(@PathVariable("catalogId") Long id) {
		JSONResult jsonResult = new JSONResult();
		try {
			catalogService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			jsonResult.setMessage(e.getMessage());
		}
		return jsonResult;
	}

	/**
	 * 通过分类id获取分类下的商品属性
	 * /catalogs/"+treeNode.id+"/propertys
	 * @return
	 */
	@GetMapping("/{catalogId}/propertys")
	private String getCatalogProperty(Model model, @PathVariable Long catalogId) {
		List<Property> propertyList = propertyService.getCatalogProperty(catalogId);
		model.addAttribute("propertyList", propertyList);
		return "property/property_list";
	}
	/**
	 * 通过分类id获取分类下的SKU属性
	 * /catalogs/"+treeNode.id+"/propertys
	 * @return
	 */
	@GetMapping("/{catalogId}/skuPropertys")
	private String getCatalogSkuProperty(Model model, @PathVariable Long catalogId) {
		List<SkuProperty> propertyList = skuPropertyService.getCatalogProperty(catalogId);
		model.addAttribute("propertyList", propertyList);
		return "sku_property/property_list";
	}

	/**
	 * 获取分类id,获取分类下的的分类属性和属性值
	 * /catalogs/"+catalogId+"/propertys/propertyValues
	 * @return
	 */

	@GetMapping("/{catalogId}/propertys/propertyValues")
	public String getPropertyValueView(Model model, @PathVariable Long catalogId) {
		List<Property> propertyList = propertyService.getCatalogProperty(catalogId);
		propertyList.forEach(property -> {
			if (property.getType() == 1) {
				List<PropertyValue> propertyValueList = propertyValueService.getPropertyValueList(property.getId());
				property.setPropertyValueList(propertyValueList);
			}
		});
		model.addAttribute("propertyList", propertyList);
		return "product/product_property_value_tr";
	}

}
